<?php
/**
 * Created by PhpStorm.
 * User: Lin
 * Date: 2017/3/11 0011
 * Time: 下午 16:19
 */
Class Goods{

    /**根据分类id查询所有商品 按销量排序
     * @param $cateArr  分类id数组
     * @return mixed
     */
    static function findGoodsInCateIdArr($cateIdArr){
        $where=array(
            'cate_id'=>array('IN',$cateIdArr),
            'status'=>3,
            'is_sale'=>1,
            'del'=>0
        );
        $goods=M('goods')->order('sort DESC')->where($where)->select();
        return $goods;
    }

    /**根据分类id查询所有商品 按指定关键字排序 带分页
     * @param $cateIdArr 分类id数组
     * @param string $order 关键字
     * @param string $limit 每页显示数量
     * @param bool $page 是否分页
     * @return array
     */
    static function findGoodsInCateIdArrByOrder($cateIdArr,$order='sort',$num='10'){
        $where=array(
            'cate_id'=>array('IN',$cateIdArr),
            'status'=>3,
            'is_sale'=>1,
            'del'=>0
        );
        $db=M('goods');
        import('ORG.Util.Page');
        $count      = $db->where($where)->count();
        $Page       = new Page($count,$num);
        $show       = $Page->show();
        $limit=$Page->firstRow.','.$Page->listRows;

        $goods=$db->order($order.' DESC')->limit($limit)->where($where)->select();

        foreach($goods as $k=>$v){
            $goods[$k]['images']=preg_match_img($v['images']);
        }
        $arr=array(
            'goods'=>$goods,
            'show'=>$show
        );
        return $arr;
    }

    /** 商品搜索
     * @param $keyWord 关键字
     * @param string $num 每页显示数量
     * @return array
     */
    static function searchGoods($keyWord,$num='10'){
        $db=M('goods');
        $where=array(
            'status'=>3,
            'is_sale'=>1,
            'del'=>0,
            'name'=>array('like','%'.$keyWord.'%')
        );

        import('ORG.Util.Page');
        $count      = $db->where($where)->count();
        $Page       = new Page($count,$num);
        $limit=$Page->firstRow.','.$Page->listRows;
        $goods=$db->where($where)->limit($limit)->select();
        foreach($goods as $k=>$v){
            $goods[$k]['images']=preg_match_img($v['images']);
        }
        $arr=array(
            'show'=> $Page->show(),
            'goods'=>$goods
         );
        return $arr;
    }
}